#include "gtest/gtest.h"

#include "common/tensor.h"
#include "common/device.h"
#include "board/cpu/cpu.h"

using namespace vision;

TEST(TensorTest, test1) {
    const std::vector<size_t> shape = {1,2,3,4};
    const FORMAT format = vision::MAJOR_ROW_NCHW;
    CPUDevice device = CPUDevice();
    vision::Tensor<float>::ptr tensor = vision::Tensor<float>::Create(shape, format, device);
    EXPECT_NE(tensor, nullptr);
    EXPECT_EQ(tensor->Eln(), 24);
    EXPECT_EQ(tensor->Eln(1), 1);
    EXPECT_EQ(tensor->Eln(2), 2);
    EXPECT_EQ(tensor->Eln(3), 6);
    EXPECT_EQ(tensor->Eln(4), 24);
    EXPECT_EQ(tensor->Bytes(), 24 * sizeof(float));
    EXPECT_EQ(tensor->Format(), format);
    for (size_t i = 0; i < shape.size(); i++) {
        EXPECT_EQ(tensor->Shape()[i], shape[i]);
    }
    // EXPECT_EQ(tensor->Buffer(), nullptr);
    EXPECT_EQ(tensor->Alloc(), SUCCESS);
    // EXPECT_NE(tensor->Buffer(), nullptr);
}

TEST(TensorMemTest, test2) {
    const std::vector<size_t> shape = {1,2,2,2};
    const FORMAT format = vision::MAJOR_ROW_NCHW;
    CPUDevice device = CPUDevice();
    vision::Tensor<float>::ptr tensor = vision::Tensor<float>::Create(shape, format, device);
    std::vector<float> data = {0,1,2,3,4,5,6,7};
    EXPECT_EQ(tensor->CopyFromAddr((char*)data.data(), data.size() * sizeof(float)), INPUT_PTR_IS_NULL);
    EXPECT_EQ(tensor->CopyFromAddr((char*)data.data(), data.size() * sizeof(float) + 1), INPUT_PTR_IS_NULL);
    tensor->Alloc();
    EXPECT_EQ(tensor->CopyFromAddr((char*)data.data(), data.size() * sizeof(float)), SUCCESS);
    vision::Tensor<float>::ptr tensor2 = vision::Tensor<float>::Create(shape, format, device);
}